home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 June: Reference Library / Dev.CD Jun 95 / Dev.CD Jun 95.toast / Technical Documentation / PCI Information / PCI Developer’s Kit (disk) / Expansion Manager / DisplayDeviceTree / DeviceTree.c next >
Encoding:
C/C++ Source or Header  |  1994-06-27  |  6.0 KB  |  202 lines  |  [TEXT/MPS ]

  1. /*
  2.     ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  3.  
  4.     File        :    DeviceTree.c
  5.     Author        :    Ben Manuto    
  6.     Creation    :    Thursday, April 7, 1994 10:49:13 AM
  7.     Copyright    :    Copyright © 1994, Apple Computer, Inc.  All rights reserved.
  8.  
  9.     Purpose        :    To Display the Expansion Manager Device Tree.    
  10.  
  11.     Revisions    :    
  12.     ---------------------------------------------------------------------------------
  13.     Date            |    Who                |    What
  14.     ---------------------------------------------------------------------------------
  15.     ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  16. */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <types.h>
  21. #include <Errors.h>
  22. #include <Memory.h>
  23. #include <String.h>
  24.  
  25. #include "ExpansionBus.h"
  26. #include "DeviceTree.h"
  27.  
  28. // •••••••••••••••••••••••••••••••••••••••••••••
  29.  
  30. main(int argc,char *argv[])
  31. {
  32. #pragma unused (argc);
  33. #pragma unused (argv);
  34.  
  35.     printf("\nDisplaying Expainsion Mgr Device Tree__________________________________\n\n");
  36.     
  37.     DisplayTree();
  38.     
  39.     return 0;
  40. }
  41.  
  42.  
  43. // •••••••••••••••••••••••••••••••••••••••••••••
  44. // DisplayTree
  45.  
  46. void DisplayTree(void)
  47. {
  48. DeviceNodeID rootNode;
  49. OSErr error;
  50. char *indentSpace;
  51.  
  52.     error = DeviceTreeRoot(&rootNode);                            // Get the root device.
  53.     if (error) {
  54.         printf("Error getting root node. err = %d\n", error);
  55.         return;
  56.     }
  57.     
  58.     indentSpace = NewPtrClear(kMaxTreeLevel * 3);                // We only want enough space for indenting kMaxTreeLevels.
  59.     ScanDeviceTree(rootNode, rootNode, indentSpace, 0);
  60.     
  61.     DisposPtr(indentSpace);                                        // Clean up our indentation string.
  62. }
  63.  
  64.  
  65. // •••••••••••••••••••••••••••••••••••••••••••••
  66. // ScanDeviceTree
  67. //
  68. // Recursively scans the device tree and displays all devices and properties.
  69.  
  70. void ScanDeviceTree(DeviceNodeID firstNode,                 // The first child node ina peer chain.
  71.                     DeviceNodeID nextNode,                    // The current node we're looking at
  72.                     char *indentSpace,                        // The spaces to indent our text.
  73.                     short treeLevel)                        // Number of child levels deep.
  74. {
  75. DeviceNodeInfo devInfo;
  76. OSErr error;
  77.  
  78.     error = DeviceTreeDeviceInfo(nextNode, &devInfo);        // Get the device info
  79.     if (error) {
  80.         printf("•• Scan: DevInfo err = %d  node = %lx\n", 
  81.                error, nextNode);
  82.         return;
  83.     }
  84.     
  85.     printf("\n%s•• Tree Level --> %d\n", indentSpace, treeLevel);
  86.     error = DisplayDeviceAndProperties(nextNode,             // Display the current device and properties.
  87.                                        indentSpace);
  88.  
  89.     if (devInfo.childNode != 0L) {                            // This node isn't it, so are there any children?    
  90.         if (treeLevel < kMaxTreeLevel)                        // Add space for new level only if we're
  91.             strcat(indentSpace, "   ");                        // not more than kMaxTreeLevel levels deep
  92.         
  93.         ScanDeviceTree(devInfo.childNode,                     // Go down to the next child node.
  94.                        devInfo.childNode, 
  95.                        indentSpace, 
  96.                        treeLevel+1);
  97.         
  98.         indentSpace[(treeLevel*3)] = 0;                        // Take away spaces for popping out a level.
  99.     }
  100.     
  101.     nextNode = devInfo.peerNode;                            // Set nextNode to the peerNode.
  102.     if (nextNode != firstNode)                                // If ==, we have checked all peer nodes in the current peer chain.
  103.         ScanDeviceTree(firstNode, 
  104.                        nextNode, 
  105.                        indentSpace, 
  106.                        treeLevel);
  107. }
  108.  
  109.  
  110.  
  111. // •••••••••••••••••••••••••••••••••••••••••••••
  112.  
  113. OSErr DumpNode(DeviceNodeID theNode, char *indentSpace)
  114. {
  115. DeviceNodeInfo theInfo;
  116. OSErr error;
  117.  
  118.     error = DeviceTreeDeviceInfo(theNode, &theInfo);
  119.     if (error) {
  120.         printf("Could not display Device Tree Info. Error = %d", error);
  121.         return error;
  122.     }
  123.     
  124.     printf("%sNode name = '%s'\n", indentSpace, theInfo.name);
  125.     printf("%sNode ID = $%08lx\n", indentSpace, theNode);
  126.     printf("%sParent = $%08lx\n", indentSpace, theInfo.parentNode);            
  127.     printf("%sPeer = $%08lx\n", indentSpace, theInfo.peerNode);            
  128.     printf("%sChild = $%08lx\n", indentSpace, theInfo.childNode);            
  129.     printf("%sAttribute of this node = $%08x\n", indentSpace, theInfo.nodeAttributes);            
  130.             
  131. }
  132.  
  133.  
  134.  
  135. // •••••••••••••••••••••••••••••••••••••••••••••
  136.  
  137. OSErr DisplayDeviceAndProperties(DeviceNodeID deviceNode, char *indentSpace)
  138. {
  139. DeviceNodeInfo devInfo;
  140. DevicePropertyInfo propInfo;
  141. DevicePropertyID firstProp, currentProp;
  142. unsigned long i, propSize;
  143. char *propData;
  144. OSErr error;
  145.  
  146.     error = DeviceTreeDeviceInfo(deviceNode, &devInfo);                // Get the device info.
  147.     if (error) return error;
  148.     
  149.     DumpNode(deviceNode, indentSpace);
  150.     
  151.     firstProp = currentProp = devInfo.propertyNode;                    // Get the first property
  152.     
  153.     do {
  154.         error = DeviceTreePropertyInfo(currentProp, &propInfo);        // Get the property info.
  155.         if (error) return error;
  156.         
  157.         printf("%s|\n",indentSpace);
  158.         printf("%s| Property Name = '%s'\n",indentSpace, propInfo.name);
  159.         printf("%s| Property node = $%08lx\n", indentSpace, devInfo.propertyNode);
  160.         printf("%s| Peer Property = $%08lx\n", indentSpace, propInfo.peerNode);
  161.         printf("%s| Property Size = %d (bytes)\n", indentSpace, propInfo.propertySize);
  162.         
  163.         if (propInfo.propertySize <= 512)                            // If Property <= 512
  164.             propSize = propInfo.propertySize;                        // Go ahead and grab the whole thing.
  165.         else
  166.             propSize = 512;                                            // else max out at 512 bytes.
  167.         
  168.         if (propInfo.propertySize <= 0)
  169.             printf("%s| There is no data for this Property!\n", indentSpace);
  170.         else {
  171.             propData = NewPtr(propSize);                            // allocate space
  172.             if (error = MemError()) {
  173.                 printf("•• Unable to Allocate space for Property Data. Err = %d\n\n", error);
  174.                 return error;
  175.             }    
  176.             
  177.             error = DeviceTreeGetProperty(currentProp, 
  178.                                           propData, 
  179.                                           propInfo.propertySize);
  180.             if (error) 
  181.                 printf("•• Could not Get Property Data. Error = %d", error);
  182.             else {
  183.                 printf("%s| Property Data (Str) = '%s'\n",             // Assumes null terminated string.    
  184.                        indentSpace, 
  185.                        propData);    
  186.                 
  187.                 printf("%s| Property Data (Bytes) = [", indentSpace);
  188.                 for (i = 0; i < propSize; i++)
  189.                     printf("$%02hx ", propData[i]);
  190.                 printf("]\n");
  191.             }
  192.                        
  193.             DisposPtr(propData);                                    // Get rid of property data.
  194.         }
  195.         currentProp = propInfo.peerNode;
  196.     } while (firstProp != currentProp);                                // If first == current, we have seen all props.
  197.  
  198.     printf("%s|__________________________________________________\n", indentSpace);
  199.     
  200.     return noErr;
  201. }
  202.